import request from './request.js'

// 获取所有商品
export function getAllProducts() {
  return request({
    url: '/Products',
    method: 'get'
  })
}

// 根据ID获取商品
export function getProductById(id) {
  return request({
    url: `/Products/${id}`,
    method: 'get'
  })
}

// 添加商品
export function addProduct(data) {
  return request({
    url: '/Products/add',
    method: 'post',
    data
  })
}

// 更新商品
export function updateProduct(id, data) {
  return request({
    url: `/Products/${id}`,
    method: 'put',
    data
  })
}

// 删除商品
export function deleteProduct(id) {
  return request({
    url: `/Products/${id}`,
    method: 'delete'
  })
}

// 根据分类获取商品
export function getProductsByCategory(categoryId) {
  return request({
    url: `/Products/category/${categoryId}`,
    method: 'get'
  })
}

// 搜索商品
export function searchProducts(name) {
  return request({
    url: '/Products/search',
    method: 'get',
    params: { name }
  })
}

// 增加商品销量
export function increaseProductSales(id, amount) {
  return request({
    url: `/Products/${id}/sales`,
    method: 'put',
    params: { amount }
  })
}

// 上传商品图片
export function uploadProductImage(productId, file) {
  const formData = new FormData()
  formData.append('file', file)
  return request({
    url: `/Products/${productId}/upload/image`,
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}